package com.scoresystem.web.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.scoresystem.common.CommonValue;
import com.scoresystem.dao.DaoFactory;
import com.scoresystem.dao.ICourseDao;
import com.scoresystem.dao.IStudentDao;
import com.scoresystem.dao.IUserDao;
import com.scoresystem.json.JsonContext;
import com.scoresystem.json.JsonState;
import com.scoresystem.model.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author Administrator
 *处理用户登录的业务逻辑类
 */
public class LoginService {
	private DaoFactory factory=DaoFactory.getInstance();
	//维护json的状态类
	private JsonState state=new JsonState();
	//根据不同的json状态选择不同的处理方法
	private JsonContext context=new JsonContext(state);
	
	/**
	 * 返回管理员登录的操作
	 * @param username
	 * @param password
	 * @return
	 */
	public JSONObject checkoutAdminLogin(String username,String password){
		IUserDao dao=factory.createUserDao();
		User admin=dao.findByNamePsd(username, password);
		try {
			if (admin == null) {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("用户名或密码不正确", null, null);
			}
			else{
				state.setState(CommonValue.STATE_SUCCESS);
				IStudentDao studao=factory.createStudentDao();
				ICourseDao coursedao=factory.createCourseDao();
				JSONArray arr=new JSONArray();
				arr.add(studao.getClasses().toArray());
				studao=factory.createStudentDao();
				arr.add(studao.getMajors().toArray());
				arr.add(coursedao.getCourses().toArray());
				return context.getObject("登录成功", null, arr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JSONObject();
	}
	
	/**
	 * 返回教师登录的操作
	 * @param username
	 * @param password
	 * @return
	 */
	public JSONObject checkoutTeaLogin(String username,String password){
		IUserDao dao=factory.createUserDao();
		User teacher=dao.findByNamePsd(username, password);
		try {
			if (teacher == null) {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("用户名或密码不正确", null, null);
			}
			else{
				state.setState(CommonValue.STATE_SUCCESS);
				IStudentDao studao=factory.createStudentDao();
				ICourseDao coursedao=factory.createCourseDao();
				JSONArray arr=new JSONArray();
				arr.add(studao.getClasses().toArray());
				arr.add(coursedao.getCoursebyTea(teacher.getUsername()).toArray());
				return context.getObject("登录成功", null, arr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JSONObject();
	}
	/**
	 * 返回学生登录的操作
	 * @param username
	 * @param password
	 * @return
	 */
	public JSONObject checkoutStuLogin(String username,String password){
		IUserDao dao=factory.createUserDao();
		User student=dao.findByNamePsd(username, password);
		try{
			if(student==null){
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("用户名或密码不正确", null, null);
			}
			else{
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("登录成功", null, null);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
	/**
	 * 修改密码业务逻辑
	 * @param username
	 * @param password
	 * @param newpsd
	 * @return
	 */
	public JSONObject checkoutUpsd(String username,String password,String newpsd){
		IUserDao dao=factory.createUserDao();
		User user=dao.findByNamePsd(username, password);
		try {
			if (user == null) {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("用户名或密码不正确", null, null);
			}
			else{
				user.setPassword(newpsd);
				dao=factory.createUserDao();
				boolean bool=dao.modify(user);
				if(bool){
					state.setState(CommonValue.STATE_SUCCESS);
					return context.getObject("修改成功", null, null);
				}
				else{
					state.setState(CommonValue.STATE_FAILED);
					return context.getObject("数据库错误", null, null);
				}
					
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new JSONObject();
	}
	
}
